package com.example.musicapi.service;

import com.example.musicapi.dao.UserDao;
import com.example.musicapi.entity.News;
import com.example.musicapi.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public User add(User s){
        return userDao.save(s);
    }
    public void delete(String id){
        userDao.deleteById(id);
    }

    public List<User> findAll(int page, int size){
        Pageable pageable = PageRequest.of(page, size);
        Page<User> userPage = userDao.findAll(pageable);
        return userPage.getContent();
    }

    public List<User> search(int page, int size, String key){
        Pageable pageable = PageRequest.of(page, size);
        Page<User> userPage = userDao.findBySearch(key,pageable);
        return userPage.getContent();
    }

    public User findById(String id) {
        return userDao.findUserByName(id);
    }

    public void update(User user){
        User d=userDao.findUserByName(user.getName()  );
        if (d!=null){
            userDao.save(user);
            return;
        }
        throw new RuntimeException("当前的:"+user.getName()+"信息中不存在");
    }
}
